#!/bin/bash
# jimgutil native 库编译脚本
# author guyadong

#字符串转大写
#参数：1字符串
function string_toupper()
{
    echo $1 | tr '[a-z]' '[A-Z]'
}
sh_folder=$(cd "$(dirname $0)"; pwd -P)
folder_name=$(basename $sh_folder)

# 定义编译的版本类型(DEBUG|RELEASE)
build_type=Release
arg1=$(string_toupper "$1")
[ "$arg1" = "DEBUG" ] && build_type="$arg1"
echo build_type=$build_type

pushd $sh_folder/.. >/dev/null

[ -d $folder_name.prj ] && rm -fr $folder_name.prj
mkdir $folder_name.prj

pushd $folder_name.prj >/dev/null

machine=$(gcc -dumpmachine)

install_prefix=$sh_folder/release/jimgutil_$machine
res_folder=$sh_folder/../jimgutil/src/main/resources/darwin-x86-64/

optsimd=
lib_suffix=
[ -n "$OPTIMIZE_SIMD" ] && optsimd="-DARCH_SIMD=$OPTIMIZE_SIMD" && lib_suffix="_$OPTIMIZE_SIMD"
cmake "$sh_folder" -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=$build_type \
	-DCMAKE_INSTALL_PREFIX=$install_prefix $optsimd

cmake --build . --target install --config release -- -j8 \
	&& mkdir -p $res_folder \
	&& cp -afv $install_prefix/lib/libjimgutil$lib_suffix.dylib $res_folder

popd >/dev/null

popd >/dev/null
